/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Helper;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 *
 * @author Marco
 */
public class ImageBase64 {

    static byte[] buffer_code = new byte[1024];

    public static String codeBase64(String i) {
        int cnt = 0;
        String s = null;
        try {
            InputStream is = new BufferedInputStream(new FileInputStream(i));
            while ((cnt = is.read(buffer_code)) != -1) {
                s = new sun.misc.BASE64Encoder().encode(buffer_code);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return s;
    }

//decode
    public static Image decodeBase64(String sb) {
        Image IMGdecode = null;
        try {
            byte[] buffer_decode = new sun.misc.BASE64Decoder().decodeBuffer(sb);
            IMGdecode = Toolkit.getDefaultToolkit().createImage(buffer_decode);

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        return IMGdecode;
    }
}
